
//
//  UserListViewController.m
//  UI-豆瓣项目APP
//
//  Created by dllo on 15/9/8.
//  Copyright (c) 2015年 孙鹏. All rights reserved.
//

#import "UserListViewController.h"
#import "LoginViewController.h"
#import "myActivityViewController.h"
#import "myMovieViewController.h"
#import "CollectMovieDB.h"
@interface UserListViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)UIButton *button;
@property(nonatomic,retain)UIAlertController *clear;
@end

@implementation UserListViewController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if([[NSUserDefaults standardUserDefaults]  objectForKey:@"LoginState"] != nil){
        NSLog(@"目前是登录状态");
        [self.button setTitle:@"注销" forState:UIControlStateNormal];
    }else{
        NSLog(@"当前不是登录状态");
        [self.button setTitle:@"登录" forState:UIControlStateNormal];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的";
     [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"bg_nav"] forBarMetrics:UIBarMetricsDefault];
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
//        [button setTitle:@"登录" forState:UIControlStateNormal];
    [self.button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.button.backgroundColor = [UIColor clearColor];
    self.button.frame = CGRectMake(0, 0, 50, 30);
    [self.button addTarget:self action:@selector(Action) forControlEvents:UIControlEventTouchUpInside];
    //    这个是带有自定义的view的
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]initWithCustomView:self.button]autorelease];
    
    UITableView *tableV = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 375, 667) style:UITableViewStylePlain];
    //    tableV.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:tableV];
    [tableV release];
    
    tableV.delegate = self;
    tableV.dataSource = self;
    
    self.clear = [UIAlertController alertControllerWithTitle:@"提示" message:@"缓存清理成功" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *ok = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction *  action){
    }];
    [self.clear addAction:ok];
    
}
-(void)Action{
    if([self.button.titleLabel.text isEqualToString:@"登录"]){
    LoginViewController *loginVC = [[LoginViewController alloc]init];
    loginVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:loginVC animated:YES];
    }else if ([self.button.titleLabel.text isEqualToString:@"注销"]){
        NSLog(@"注销了");
        [self.button setTitle:@"登录" forState:UIControlStateNormal];
        [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"LoginState"];
    }
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifer = @"liuxin";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
    if(cell == nil){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
    }
    if(indexPath.row == 0){
        cell.textLabel.text = @"我的活动";
    }
    if(indexPath.row == 1){
        cell.textLabel.text = @"我的电影";
    }
    if(indexPath.row == 2){
    cell.textLabel.text = @"清除缓存";
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSUserDefaults *allInfo =[NSUserDefaults standardUserDefaults];
    if(([allInfo objectForKey:@"LoginState"] == nil)){
    LoginViewController *loginVC = [[LoginViewController alloc]init];
    loginVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:loginVC animated:YES];
    }else{
        NSLog(@"LoginState == %@",[allInfo objectForKey:@"LoginState"]);
        NSLog(@"已经登陆过了");
        if (indexPath.row == 0) {
            myActivityViewController *myActivity = [[myActivityViewController alloc]init];
            [self.navigationController pushViewController:myActivity animated:YES];
        }else if (indexPath.row == 1){
            myMovieViewController *myMovie = [[myMovieViewController alloc]init];
            [self.navigationController pushViewController:myMovie animated:YES];
        }else if(indexPath.row == 2){
            [self clearMemery];
        }
    }
}
-(void)clearMemery{
    [[CollectMovieDB shareDataBase]dropTable];
    [[CollectMovieDB shareDataBase]createTable];
    [self presentViewController:self.clear animated:YES completion:^{
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
